package com.basic.hellorabbit.controller;

import com.basic.hellorabbit.util.DfsClientUtil;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;


/**
* @Description:    fastDfs接口
* @Author:         Joe
* @CreateDate:     2020/3/19 13:02
*/
@Controller
public class DfsController {

    @Autowired
    private FastFileStorageClient fileStorageClient;

    @Autowired
    private DfsClientUtil dfsClientUtil;

    /**
     * 跳转到fastDfs测试页面
     * @return
     */
    @RequestMapping(value = "/dfs",method = RequestMethod.GET)
    public String toDfs(){
        return "dfs";
    }

    @ResponseBody
    @RequestMapping(value = "/dfsupload",method = RequestMethod.POST)
    public String upload(MultipartFile file) throws IOException {
        String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        System.out.println("文件后缀名:"+suffixName);
        StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), suffixName, null);
        String fullPath = storePath.getFullPath();
        if(!StringUtils.isEmpty(fullPath)){
            System.out.println("fullPath:"+fullPath);
            String completeURl = dfsClientUtil.getCompleteURl(fullPath);
            System.out.println("completeURl:"+completeURl);
            return "上传完成"+completeURl;
        }
        return "上传失败";

    }

    /**
     * 文件下载
     * @param filePath
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public void download(String filePath, HttpServletRequest request , HttpServletResponse response) throws IOException {

        String[] paths = filePath.split("/");
        String groupName = null ;
        for (String item : paths) {
            if (item.indexOf("group1") != -1) {
                groupName = item;
                break ;
            }
        }
        String path = filePath.substring(filePath.indexOf(groupName) + groupName.length() + 1, filePath.length());
        InputStream inputStream = fileStorageClient.downloadFile(groupName, path, new DownloadCallback<InputStream>() {
            @Override
            public InputStream recv(InputStream inputStream) throws IOException {
                return inputStream;
            }
        });

        //根据文件名获取 MIME 类型
        String fileName = paths[paths.length-1] ;
        System.out.println("fileName :" + fileName);
        String contentType = request.getServletContext().getMimeType(fileName);
        String contentDisposition = "attachment;filename=" + fileName;

        // 设置头
        response.setHeader("Content-Type",contentType);
        response.setHeader("Content-Disposition",contentDisposition);

        // 获取绑定了客户端的流
        ServletOutputStream output = response.getOutputStream();

        // 把输入流中的数据写入到输出流中
        IOUtils.copy(inputStream,output);
        inputStream.close();
    }

    /**
     * 文件下载
     * @param filePath
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delfile",method = RequestMethod.GET)
    public String delfile(String filePath){
        String[] paths = filePath.split("/");
        String groupName = null ;
        for (String item : paths) {
            if (item.indexOf("group1") != -1) {
                groupName = item;
                break ;
            }
        }
        String path = filePath.substring(filePath.indexOf(groupName) + groupName.length() + 1, filePath.length());
        fileStorageClient.deleteFile(groupName,path);
        return "成功删除"+filePath;
    }
}
